﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Capstone_project.DatabaseAccessLayer;


namespace Capstone_project
{
    public partial class frmUpdateUserTask : Form
    {
        public static string idtask_fr_taskmanagement;
        public static string username_Us_Ta;
        SqlConnection con = Common.con;
        SqlDataAdapter adap;
        DataTable dt = new DataTable();
        public frmUpdateUserTask()
        {
            InitializeComponent();
        }

        private void frmUpdateUserTask_Load(object sender, EventArgs e)
        {
            dt.Rows.Clear();
            #region fill data into textbox
            string sql = "SELECT     dbo.TASK.ID_TASK, dbo.TASK.NAME_TASK, dbo.PROJECT.NAME_PROJECT, dbo.TASK_GROUP.NAME_TASK_GROUP, dbo.PRIORITY.NAME_PRIORITY, "
            +"          dbo.TASK.TIMES, dbo.PROJECT.START_DATE, dbo.PROJECT.END_DATE, dbo.ASSIGN_TASK_STATUS.ID_TASK_STATUS, dbo.USER_TASK.USER_NAME, "
            +"          dbo.USER_TASK.COMPLETE, dbo.USER_TASK.DESCRIPTION"
            +"  FROM         dbo.PROJECT INNER JOIN"
            +"          dbo.TASK_GROUP ON dbo.PROJECT.ID_PROJECT = dbo.TASK_GROUP.ID_PROJECT INNER JOIN"
            +"          dbo.TASK ON dbo.TASK_GROUP.ID_TASK_GROUP = dbo.TASK.ID_TASK_GROUP INNER JOIN"
            +"          dbo.ASSIGN_TASK_STATUS ON dbo.TASK.ID_TASK_STATUS = dbo.ASSIGN_TASK_STATUS.ID_TASK_STATUS INNER JOIN"
            +"          dbo.USER_TASK ON dbo.TASK.ID_TASK = dbo.USER_TASK.ID_TASK INNER JOIN"
            +"          dbo.PRIORITY ON dbo.TASK.ID_PRIORITY = dbo.PRIORITY.ID_PRIORITY"
            +"  WHERE     (dbo.USER_TASK.USER_NAME = '"+username_Us_Ta.Trim()+"') AND (dbo.USER_TASK.ID_TASK = '"+idtask_fr_taskmanagement.Trim()+"')";
            if (con.State != ConnectionState.Open)
                con.Open();
            SqlCommand cmd = new SqlCommand(sql,con);
            adap = new SqlDataAdapter(cmd);
            adap.Fill(dt);
            con.Close();

            txtTaskID.Text = idtask_fr_taskmanagement;
            txtTaskname.Text= dt.Rows[0][1].ToString();
            txtProject.Text = dt.Rows[0][2].ToString();
            txtModule.Text = dt.Rows[0][3].ToString();
            txtPriority.Text = dt.Rows[0][4].ToString();
            txtHours.Text = dt.Rows[0][5].ToString();
            txtStartdate.Text = dt.Rows[0][6].ToString();
            txtEnddate.Text = dt.Rows[0][7].ToString();
            txtStatus.Text = dt.Rows[0][8].ToString();
            txtStaff.Text = username_Us_Ta;
            txtComplete.Text = dt.Rows[0][10].ToString();
            txtComment.Text = dt.Rows[0][11].ToString();
            #endregion

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (con.State != ConnectionState.Open)
                con.Open();
            SqlCommand cmd = new SqlCommand("Update_UserTask",con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@idtask", SqlDbType.NChar).Value = txtTaskID.Text.Trim();
            cmd.Parameters.Add("@username", SqlDbType.NVarChar).Value = txtStaff.Text.Trim();
            cmd.Parameters.Add("@complete", SqlDbType.Real).Value = txtComplete.Text.Trim();
            cmd.Parameters.Add("@comment", SqlDbType.NVarChar).Value = txtComment.Text.Trim();
            int kq = cmd.ExecuteNonQuery();
            con.Close();
            if (kq != 0)
            {
                MessageBox.Show("Update infor successfully.");
                this.Close();
            }
        }
    }
}
